package de.tabuma.pm.security.controller;

import de.tabuma.lib.contentmanagement.*;
import de.tabuma.ssoclient.entity.*;
import de.tabuma.ssoclient.logik.*;
import java.util.*;
import java.util.logging.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/04 10:24:02 $
 */
public class UserFacade implements FacadeInterface {

    private static Logger log = Logger.getLogger(UserFacade.class.getName());

    String ssoId;
    public String getSsoId() {
        return ssoId;
    }
    public void setSsoId(String ssoId) {
        this.ssoId = ssoId;
    }

    int groupId;
    public int getGroupId() {
        return groupId;
    }
    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }


    public int createElement(Object object, String user) {
        try {
            int userId = de.tabuma.ssoclient.logik.UserFacade.create((User) object, ssoId);
            UserGroup userGroup = new UserGroup();
            userGroup.setGroupId(groupId);
            userGroup.setUserId(userId);
            return de.tabuma.ssoclient.logik.UserGroupFacade.create(userGroup, ssoId);
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return 0;
    }

    public Object getElement(int id) {
        try {
            UserGroup ug = UserGroupFacade.get(id, ssoId);
            return de.tabuma.ssoclient.logik.UserFacade.get(ug.getUserId(), ssoId);
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return null;
    }

    public List getElementList(String listParameter) {
        try {
            List<UserGroup> list = de.tabuma.ssoclient.logik.UserGroupFacade.getList(groupId, ssoId);
            List<User> listUser = new ArrayList<User>();
            for(int i=0;i<list.size();i++) {
                UserGroup ug = list.get(i);
                User user1 = new User();
                User user = de.tabuma.ssoclient.logik.UserFacade.get(ug.getUserId(), ssoId);
                user1.setId(ug.getId());
                user1.setName(user.getName());
                user1.setPassword(user.getPassword());
                listUser.add(user1);
            }
            return listUser;
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return null;
    }

    public void removeElement(int id, String user) {
        try {
            UserGroup ug = UserGroupFacade.get(id, ssoId);
            de.tabuma.ssoclient.logik.UserFacade.remove(ug.getUserId(), ssoId);
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }

    public void updateElement(Object object, String user) {
        try {
            User user1 = (User) object;
            UserGroup ug = UserGroupFacade.get(user1.getId().intValue(), ssoId);
            user1.setId(new Long(ug.getUserId()));
            de.tabuma.ssoclient.logik.UserFacade.update((User)object, ssoId);
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}